package vip.liux.contracts.infrastructure.converts;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.time.Duration;

@Converter(autoApply = true)
public class DurationSecondsConverter implements AttributeConverter<Duration, Integer> {

    @Override
    public Integer convertToDatabaseColumn(Duration duration) {
        return duration == null ? null : (int) duration.getSeconds();
    }

    @Override
    public Duration convertToEntityAttribute(Integer dbData) {
        return dbData == null ? null : Duration.ofSeconds(dbData);
    }
}